package com.education.platform.management.service.impl;

import com.education.platform.common.enums.QuestionModeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.QuestionBankDao;
import com.education.platform.dao.entity.QuestionBankDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.service.QuestionBankService;
import com.education.platform.management.vo.request.QuestionBankUpdateVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * describe: 题库
 *
 * @author : tomato
 * @date: 2020-06-29 10:28:07.918
 **/
@Service
public class QuestionBankServiceImpl extends BaseServiceImpl<QuestionBankDao, QuestionBankDO> implements QuestionBankService {

    private static final Logger LOGGER = LoggerFactory.getLogger(QuestionBankServiceImpl.class);

    @Autowired
    private QuestionBankDao questionBankDao;


    @Override
    public Boolean save(QuestionBankUpdateVO vo) {
        this.setMode(vo);
        this.check(vo);
        //        //转换  类型
        QuestionBankDO questionBankDO = BeanUtil.convert(vo, QuestionBankDO.class);

        if (vo.getType() != 2) {
            this.setOption(vo, questionBankDO);
        } else {
            questionBankDO.setChoiceAnswer(vo.getShortAnswer());
        }
        if (questionBankDO.getId() == null) {
            questionBankDO.setCreateBy(ThreadUser.getUserName());
            questionBankDO.setCreateTime(new Date());
            return questionBankDao.insert(questionBankDO) == 1;
        } else {
            questionBankDO.setUpdateBy(ThreadUser.getUserName());
            questionBankDO.setUpdateTime(new Date());
            return questionBankDao.updateById(questionBankDO) == 1;
        }
    }

    private void setOption(QuestionBankUpdateVO vo, QuestionBankDO questionBankDO) {
        //非简答题  有选项  和 答案
        if (CollectionUtils.isNotEmpty(vo.getOptions())) {
            questionBankDO.setOptionA(vo.getOptions().parallelStream().collect(Collectors.joining("==")));
        } else {
            throw new BusinessException("选项为空");

        }
        if (CollectionUtils.isNotEmpty(vo.getAnswerIndexList())) {
            questionBankDO.setChoiceAnswer(vo.getAnswerIndexList().parallelStream().map(String::valueOf).collect(Collectors.joining("==")));
        } else {
            throw new BusinessException("答案为空");
        }
    }

    private void setMode(QuestionBankUpdateVO vo) {
        if(CollectionUtils.isEmpty(vo.getModes())){
            throw new BusinessException("题目类型不能必选");
        }
        String mode = vo.getModes().parallelStream().map(m->m+"").collect(Collectors.joining(","));
        vo.setMode(mode);
    }

    private void check(QuestionBankUpdateVO vo) {
        if (vo == null) {
            throw new BusinessException("数据为空");
        }
        if (vo.getType() == null) {
            throw new BusinessException("题目类型为空");
        }

        if (StringUtils.isEmpty(vo.getMode())) {
            throw new BusinessException("请选择题目类型");
        }
    }
}
